/**
 * @Author houlijx
 * @Description //TODO $
 * @Date 2021/8/11 下午4:24
 **/
package sysrep

import (
	"github.com/gogf/gf/os/gproc"
	"os/exec"
)

type CmdResult struct {
	Result bool     `json:"result"`
	Output string   `json:"output"`
	Errors []string `json:"errors"`
}

func ExecuteCmd(cmd string) *CmdResult {
	var result CmdResult
	cmdResult := exec.Command("/bin/bash", "-c", cmd)
	output, err := cmdResult.Output()
	result.Output = string(output)
	if err != nil {
		result.Result = false
		result.Errors = append(result.Errors, err.Error())
	}
	return &result
}

// ExecuteCmdByGproc
// 执行命令
func ExecuteCmdByGproc(cmd string) *CmdResult {
	var result CmdResult
	output, err := gproc.ShellExec(cmd)
	result.Output = output
	if err != nil {
		result.Result = false
		result.Errors = append(result.Errors, err.Error())
	} else {
		result.Result = true
	}
	return &result
}
